<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Top 25 factions' );

$query = "SELECT name, score, leader, faction_id, type_name, control_type " .
		"FROM faction " .
		"JOIN faction_types ON faction.faction_type_id = faction_types.faction_type_id " .
		"ORDER BY score DESC LIMIT 0, 25";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);

if ($num > 0) {
	if (isset ($_SESSION['faction_ranks_status'])) {
		print $_SESSION['faction_ranks_status'];
		unset ($_SESSION['faction_ranks_status']);
	}
?>    
					<div class="blok">
						<?php transprint( 'Get information on a faction' ); ?>: 
						<form method="post" action="forward_faction.php">
    						<input type="text" name="faction_name" />
		  					<input type="submit" value="submit" />
		  				</form>
		  			</div>
		  			<br />
					<table>
						<tr><th colspan="8"><?php transprint( 'Top 25 factions' ); ?>:</th></tr>
    					<tr>
    						<td><?php transprint( 'Rank' ); ?></td>
    						<td><?php transprint( 'Structure' ); ?></td>
    						<td><?php transprint( 'Name' ); ?></td>
    						<td><?php transprint( 'Leader' ); ?></td>
    						<td><?php transprint( 'Color' ); ?></td>
    						<td align="right"><?php transprint( 'Score' ); ?></td>
    						<td align="right"><?php transprint( 'Average score' ); ?></td>
    						<td align="right"><?php transprint( 'Members' ); ?></td>
    					</tr>
<?php

	$i = 0;
	while ($i < $num) {
		if ($i % 2 == 0) {
			print "<tr class=\"even\">";
		} else {
			print "<tr>";
		}
		$query = "SELECT SUM(experience) " .
				"FROM ruler " .
				"JOIN faction_member ON faction_member.ruler_id = ruler.ruler_id " .
				"WHERE faction_id = " . mysql_result($result, $i, "faction_id") . " AND requested = 0 AND invited = 0";
		$result2 = mysql_query($query) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$combat_score = $row2['SUM(experience)'];

		$color = get_faction_color( mysql_result($result, $i, "faction_id" ) );
		
		$query = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result($result, $i, "leader");
		$result2 = mysql_query($query) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$leader = $row2['name'];

		$query = "SELECT count(*) FROM faction WHERE score > " . mysql_result($result, $i, "score");
		$result2 = mysql_query($query) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$rank = number_format($row2['count(*)'] + 1);

		$query = "SELECT count(*) FROM faction_member WHERE faction_id = " . mysql_result($result, $i, "faction_id") . " AND requested = 0 AND invited = 0";
		$result2 = mysql_query($query) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$members = $row2['count(*)'];
		
		if( mysql_result($result, $i, 'control_type' ) == 0 ) {
			$control_type_string = translate( 'Hierarchic' );
		}
		else {
			$control_type_string = translate( 'Democratic' );
		}
		print "<td align=\"right\">" . $rank . "</td>" .
				"<td>$control_type_string</td>" .
				"<td>" . translate(mysql_result($result, $i, "type_name")) . ": <a href=\"faction.php?name=" . mysql_result($result, $i, "name") . "\">" . mysql_result($result, $i, "name") . "</a></td>" .
				"<td><a href=\"ruler.php?name=$leader\">" . $leader . "</a></td>" .
				"<td>$color</td>" .
				"<td align=\"right\">" . number_format(mysql_result($result, $i, "score")) . "</td>" .
				"<td align=\"right\">" . number_format(round(mysql_result($result, $i, "score") / $members, 0)) . "</td>" .
				"<td align=\"right\">" . $members . "</td>" .
				"</tr>\r\n";
		$i++;
	}
?>
    			</table>
<?php

} else {
	print ("<div class=\"blok\">\r\n");
	print (translate('There are no factions at the moment. ') . "<br />\r\n");
	print ("</div>\r\n");
}
aos_footer();
?>
